home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
OLE2BOOK.ZIP
/
CHAP08.ZIP
/
CHAP08
/
PATRON
/
DOCUMENT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-07
|
23KB
|
1,031 lines
/*
* DOCUMENT.CPP
* Modifications for Chapter 8
*
* Implementation of the CPatronDoc derivation of CDocument that
* manages pages for us.
*
* Copyright (c)1993 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Software Design Engineer
* Microsoft Systems Developer Relations
*
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include <memory.h>
#include "patron.h"
/*
* CPatronDoc::CPatronDoc
* CPatronDoc::~CPatronDoc
*
* Constructor Parameters:
* hInst HINSTANCE of the application.
*/
CPatronDoc::CPatronDoc(HINSTANCE hInst)
: CDocument(hInst)
{
m_pPG=NULL;
m_lVer=VERSIONCURRENT;
m_pIStorage=NULL;
m_fPrintSetup=TRUE;
//CHAPTER8MOD
m_pDropTarget=NULL;
//End CHAPTER8MOD
return;
}
CPatronDoc::~CPatronDoc(void)
{
//CHAPTER8MOD
if (NULL!=m_pDropTarget)
{
RevokeDragDrop(m_hWnd);
CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, FALSE, TRUE);
m_pDropTarget->Release();
}
//End CHAPTER8MOD
if (NULL!=m_pPG)
delete m_pPG;
if (NULL!=m_pIStorage)
m_pIStorage->Release();
CoFreeUnusedLibraries();
return;
}
/*
* CPatronDoc::FInit
*
* Purpose:
* Initializes an already created document window. The client actually
* creates the window for us, then passes that here for further
* initialization.
*
* Parameters:
* pDI LPDOCUMENTINIT containing initialization parameters.
*
* Return Value:
* BOOL TRUE if the function succeeded, FALSE otherwise.
*/
BOOL CPatronDoc::FInit(LPDOCUMENTINIT pDI)
{
//Change the stringtable range to our customization.
pDI->idsMin=IDS_DOCUMENTMIN;
pDI->idsMax=IDS_DOCUMENTMAX;
//Do default initialization
if (!CDocument::FInit(pDI))
return FALSE;
//Pages are created when we get a ::ULoad later.
return TRUE;
}
/*
* CPatronDoc::FMessageHook
*
* Purpose:
* Processes WM_SIZE for the document so we can resize the Pages window.
*
* Parameters:
* <WndProc Parameters>
* pLRes LRESULT FAR * in which to store the return value
* for the message.
*
* Return Value:
* BOOL TRUE to prevent further processing, FALSE otherwise.
*/
BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
, LPARAM lParam, LRESULT FAR *pLRes)
{
UINT dx, dy;
RECT rc;
if (WM_SIZE==iMsg && NULL!=m_pPG)
{
dx=LOWORD(lParam);
dy=HIWORD(lParam);
//Resize the Pages window to fit the new client area of the document
GetClientRect(hWnd, &rc);
m_pPG->RectSet(&rc, FALSE);
}
/*
* We return FALSE even on WM_SIZE so we can let the default procedure
* handle maximized MDI child windows appropriately.
*/
return FALSE;
}
/*
* CPatronDoc::Clear
*
* Purpose:
* Sets all contents in the document back to defaults with no filename.
*
* Paramters:
* None
*
* Return Value:
* None
*/
void CPatronDoc::Clear(void)
{
//Completely reset the pages
m_pPG->FIStorageSet(NULL, FALSE, FALSE);
CDocument::Clear();
m_lVer=VERSIONCURRENT;
return;
}
/*
* CPatronDoc::FDirtyGet
*
* Purpose:
* Returns the current dirty status of the document.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if the file is clean, FALSE otherwise.
*/
BOOL CPatronDoc::FDirtyGet()
{
BOOL fPageDirty;
fPageDirty=m_pPG->FIsDirty();
return m_fDirty | fPageDirty;
}
/*
* CPatronDoc::Delete
*
* Purpose:
* Removed the current object from the document.
*
* Paramters:
* None
*
* Return Value:
* None
*/
void CPatronDoc::Delete(void)
{
if (NULL!=m_pPG)
m_pPG->TenantDestroy();
CoFreeUnusedLibraries();
return;
}
/*
* CPatronDoc::FQueryPrinterSetup
*
* Purpose:
* Returns whether or not the Printer Setup menu item can be
* enabled. Once you create a tenant in any page, Printer Setup
* is voided simply to keep this sample simple, that is, we don't
* have to worry about reorganizing potentially large amounts
* of layout after we start plopping down objects.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE to enable the menu, FALSE otherwise.
*/
BOOL CPatronDoc::FQueryPrinterSetup(void)
{
return m_fPrintSetup;
}
/*
* CPatronDoc::FQueryObjectSelected
*
* Purpose:
* Returns whether or not there is an object selected in this
* document for Cut, Copy, Delete functions.
*
* Parameters:
* hMenu HMENU of the Edit menu.
*
* Return Value:
* BOOL TRUE if we have an object, FALSE otherwise.
*/
BOOL CPatronDoc::FQueryObjectSelected(HMENU hMenu)
{
return m_pPG->FQueryObjectSelected(hMenu);
}
/*
* CPatronDoc::ULoad
*
* Purpose:
* Loads a given document without any user interface overwriting the
* previous contents of the editor.
*
* Parameters:
* fChangeFile BOOL indicating if we're to update the window title
* and the filename from using this file.
* pszFile LPSTR to the filename to load. Could be NULL for
* an untitled document.
*
* Return Value:
* UINT An error value from DOCERR_*
*/
UINT CPatronDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
{
RECT rc;
LPSTORAGE pIStorage;
HRESULT hr;
CLSID clsID;
DWORD dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
if (NULL==pszFile)
{
//Create a new temp file.
hr=StgCreateDocfile(NULL, dwMode | STGM_CREATE | STGM_DELETEONRELEASE
, 0, &pIStorage);
//Mark this as one of our class since we check with ReadClassStg below.
if (SUCCEEDED(hr))
WriteClassStg(pIStorage, CLSID_PatronPages);
}
else
{
hr=StgOpenStorage(pszFile, NULL, dwMode, NULL, 0, &pIStorage);
}
if (FAILED(hr))
return DOCERR_COULDNOTOPEN;
//Check if this is our type of file and exit if not.
hr=ReadClassStg(pIStorage, &clsID);
if (FAILED(hr) || !IsEqualCLSID(clsID, CLSID_PatronPages))
{
pIStorage->Release();
return DOCERR_READFAILURE;
}
//Attempt to create our contained Pages window.
m_pPG=new CPages(m_hInst, m_cf);
GetClientRect(m_hWnd, &rc);
if (!m_pPG->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_PAGES, NULL))
{
pIStorage->Release();
return DOCERR_READFAILURE;
}
if (!m_pPG->FIStorageSet(pIStorage, FALSE, (BOOL)(NULL==pszFile)))
{
pIStorage->Release();
return DOCERR_READFAILURE;
}
//CHAPTER8MOD
//Open the window up for drag-drop
m_pDropTarget=new CDropTarget(this);
if (NULL!=m_pDropTarget)
{
m_pDropTarget->AddRef();
RegisterDragDrop(m_hWnd, (LPDROPTARGET)m_pDropTarget);
CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, TRUE, FALSE);
}
//End CHAPTER8MOD
m_pIStorage=pIStorage;
Rename(pszFile);
//Do initial setup if this is a new file, otherwise Pages handles things.
if (NULL==pszFile)
{
//Go initialize the Pages for the default printer.
if (!PrinterSetup(NULL, TRUE))
return DOCERR_COULDNOTOPEN;
//Go create an initial page.
m_pPG->PageInsert(0);
}
else
m_fPrintSetup=FALSE; //Can't change an already saved configuration
FDirtySet(FALSE);
return DOCERR_NONE;
}
/*
* CPatronDoc::USave
*
* Purpose:
* Writes the file to a known filename, requiring that the user has
* previou